package com.hangwei.aicabinet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.example.qrlibrary.qrcode.utils.QRCodeUtil;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.model.bean.Login;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;

import rx.Observable;

/**
 * author: 张龙臣
 * date: 2018-08-28.
 */

public class QRActivity extends BaseActivity {

    private View llView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr);
        llView = findViewById(R.id.llView);
        ImageView ivQr = findViewById(R.id.ivQr);

        String id = SPUtil.getString(SPKey.CABINET_ID);
        ivQr.setImageBitmap(QRCodeUtil.CreateTwoDCode("cabinetId:" + id + ",key:" + "XIANPADLOGIN"));

        listener(id);
        App.getmHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                App.getmHandler().removeCallbacksAndMessages(null);
                finish();
            }
        },30*1000);
    }

    private void listener(final String id) {
        NetManager.getInstance().doSubscribe(new NetManager.Model<Login>() {
            @Override
            protected Observable<ResponseInfo<Login>> getObservable() {
                return NetManager.getResponseInfoAPI().listenLogin(id);
            }

            @Override
            protected void onNext(Login data) {
                if ("1".equals(data.status)) {
                    ToastUtil.showCenterToast("登录成功");
                    SPUtil.setString(SPKey.Account.USER_ID, data.userId);
                    SPUtil.setString(SPKey.Account.PERSON_ID, data.personId);
//                    if (Build.VERSION.SDK_INT >= 21)
//                        startActivity(new Intent(QRActivity.this, MainActivity.class),
//                                ActivityOptions.makeSceneTransitionAnimation(QRActivity.this).toBundle());
//                    else
                    if ("1".equals(data.isWarehouseAdmin))
                        startActivity(new Intent(QRActivity.this, AdminActivity.class));
                    else startActivity(new Intent(QRActivity.this, MainActivity.class));

                    finish();
                } else {
                    App.getmHandler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            listener(id);
                        }
                    }, 500);
                }
            }

            @Override
            protected void onError() {
                ToastUtil.showCenterToast("网络错误");
                finish();
            }
        });
    }

//    private void countDown(final int i) {
//        App.getmHandler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                ToastUtil.showCenterToast("" + i);
//                if (i == 0) {
//                    if (Build.VERSION.SDK_INT >= 21)
//                        startActivity(new Intent(QRActivity.this, MainActivity.class),
//                                ActivityOptions.makeSceneTransitionAnimation(QRActivity.this).toBundle());
//                    else startActivity(new Intent(QRActivity.this, MainActivity.class));
////                    startActivity(new Intent(QRActivity.this, MainActivity.class));
////                    overridePendingTransition(R.anim.in,R.anim.out);
//                    return;
//                }
//                countDown(i - 1);
//            }
//        }, 1000);
//    }
}
